package com.situ.mall.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.mall.common.ServerResponse;
import com.situ.mall.entity.User;
import com.situ.mall.mapper.UserMapper;
import com.situ.mall.service.IUserService;
@Service
public class UserServiceImpl implements IUserService{
	@Autowired
	private UserMapper userMapper;

	@Override
	public ServerResponse login(String username, String password) {
		//判断这个用户名存不存在
		int count = userMapper.checkUserName(username);
		System.out.println("查找用户名"+count);
		if (count == 0) {
			return ServerResponse.createError("用户名不存在");
		}
		
		User user = userMapper.selectLogin(username, password);
		System.out.println("查找密码"+user);
		if (user == null) {
			return ServerResponse.createError("密码错误");
		}
		//?
		user.setPassword("");
		
		return ServerResponse.createSuccess("登录成功", user);
	}

	@Override
	public ServerResponse pageList(Integer page, Integer limit) {
		//1、使用PageHelper插件设置分页
		PageHelper.startPage(page, limit);
		//2、执行查询
		List<User> list = userMapper.pageList();
		//3、使用PageInfo对结果进行包装
		PageInfo pageInfo = new PageInfo(list);
		Integer count = (int) pageInfo.getTotal();//得到总数量
		
		return ServerResponse.createSuccess("查询成功", count, list);
	}
}
